package menuTree;

import java.io.Serializable;
import java.security.Principal;
import java.util.ArrayList;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.event.NodeCollapseEvent;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.TreeNode;

@ManagedBean
@SessionScoped
public class TreeBean implements Serializable {
 
    private static final long serialVersionUID = 2417620239014385855L;
    private TreeNodeImpl root;
    private TreeNode[] selectedNodes;
    MenuTreeDAO objectDAO;
    private Integer count;
 
    /**
     * Default constructor
     */
    public TreeBean() {
        root = new TreeNodeImpl(0, 0,"Root", null);
       objectDAO = new MenuTreeDAO();
      
       prepareChilds(root);
    }
    
    public void deleteSelectedNodes(ActionEvent event){
    	System.out.println("delete category");
    	objectDAO.deleteNodes(selectedNodes);
    	 root = new TreeNodeImpl(0, 0,"Root", null);
         prepareChilds(root);
    }
    
    public void checkPrincipal(ActionEvent event){
    	Principal p = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
    	System.out.println("user  : " + p.getName());
    }
    
    public void moveCatToParenAndDelete(ActionEvent event){
    	System.out.println("moveCatToParenAndDelete");
    	
    	objectDAO.moveCatToParentAndDelete(selectedNodes);
    	root = new TreeNodeImpl(0, 0,"Root", null);
        prepareChilds(root);
    }
    
    public void prepareHead(){
		
	}
	
	private void prepareChilds(TreeNodeImpl d){
		
		ArrayList<TreeNodeImpl> list = objectDAO.getCatsByParent(d);
	//	if(list == null || list.size()==0)
		//	return null;
	/*	for(TreeNodeImpl data : list){
			//System.out.println("id : " + data.getId() + " parent_id: " + data.getParentId() + " name:" + data.getName());
			prepareChilds(data);
			if(tmp==null)
				data.setChild(null);
			else 
				data.setChild(tmp);
			
		}*/
		//return list;
	}
	

    
 
    /**
     * This method returns the tree model based on the root node.
     *
     * @return root node.
     */
    public TreeNode getModel() {
        return root;
    }
 

    /**
     * {@inheritDoc }
     *
     * Adds a {@link javax.faces.application.FacesMessage} with event data to
     * the {@link javax.faces.context.FacesContext}.
     */
    public void onNodeSelect(NodeSelectEvent event) {
    	 count = selectedNodes.length;
    	 for(TreeNode data : selectedNodes){
    		 if(data.getChildCount()==0)
    		 objectDAO.getCatsByParent((TreeNodeImpl) data);
    		 data.setExpanded(true);
    	 }
        
        //FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", event.getTreeNode().getData().toString());
        //FacesContext.getCurrentInstance().addMessage(event.getComponent().getId(), msg);
    }
 
    /**
     * {@inheritDoc}
     *
     * Adds a {@link javax.faces.application.FacesMessage} with event data to
     * the {@link javax.faces.context.FacesContext}.
     */
    public void onNodeExpand(NodeExpandEvent event) {
 
        System.out.println("NodeExpandEvent Fired");
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Expanded", event.getTreeNode().getData().toString());
        FacesContext.getCurrentInstance().addMessage(event.getComponent().getId(), msg);
    }
 
    /**
     * {@inheritDoc}
     *
     * Adds a {@link javax.faces.application.FacesMessage} with event data to
     * the {@link javax.faces.context.FacesContext}.
     */
    public void onNodeCollapse(NodeCollapseEvent event) {
        System.out.println("NodeCollapseEvent Fired");
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Collapsed", event.getTreeNode().getData().toString());
        FacesContext.getCurrentInstance().addMessage(event.getComponent().getId(), msg);
    }
    
    public TreeNode[] getSelectedNodes() {  
        return selectedNodes;  
    }  
  
    public void setSelectedNodes(TreeNode[] selectedNodes) {  
        this.selectedNodes = selectedNodes;  
    }

	public Integer getCount() {
		return count;
	}  
    
}